package com.doit.day03;

import com.alibaba.fastjson.JSON;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.roaringbitmap.RoaringBitmap;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

//1.每5s输出一次当前来了多少用户(去重)  uv
public class _02_消费者练习题4_新老用户 {
    public static void main(String[] args) {
        //创建一个bloomFilter对象
        BloomFilter<Long> longBloomFilter = BloomFilter.create(Funnels.longFunnel(), 100000, 0.1);

        //读取数据的线程
        new Thread(new MyThread_bloomFilter(longBloomFilter,"event-log")).start();

    }
}

class MyThread_bloomFilter implements Runnable{
    private BloomFilter<Long> longBloomFilter;
    private KafkaConsumer<String, String> consumer;
    public MyThread_bloomFilter(BloomFilter<Long> longBloomFilter,String ... topics ){
        this.longBloomFilter = longBloomFilter ;
        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "HaoRang1");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        //创建kafka的消费者
        consumer = new KafkaConsumer<String, String>(props);
        consumer.subscribe(Arrays.asList(topics));
    }
    @Override
    public void run() {

        //拉取数据
        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Integer.MAX_VALUE));
            for (ConsumerRecord<String, String> record : poll) {
                String json = record.value();
                EventLog eventLog = JSON.parseObject(json, EventLog.class);
                int guid = eventLog.getGuid();
                //判断一下，这个guid之前出现过吗？
                //如果之前出现过，就是老用户，如果没有出现过，就是新用户
                if (longBloomFilter.mightContain((long)guid)){
                    //出现过，所以标记为老用户
                    eventLog.setIsNew(0);
                    String jsonString = JSON.toJSONString(eventLog);
                    System.out.println(jsonString);
                }else {
                    //没有出现过，标记为新用户
                    eventLog.setIsNew(1);
                    //如果是一个新用户，那么需要你重新做一个标注，将新用户标注进去，便于下一次查询判断
                    longBloomFilter.put((long)guid);
                    String jsonString = JSON.toJSONString(eventLog);
                    System.out.println(jsonString);
                }
            }
        }
    }
}

